// ctrls/users.js
const db = require('../common/db');
const sql = require('../common/sql');
const mytoken = require('../token/token');

module.exports = {
    login: (req, res) => {
        let username = req.body.username;
        let phone = req.body.phone;
        let pwd = req.body.pwd;
        db.query(sql.users.login, [username, phone, pwd], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: '查询数据库错误！', err: err });
            } else {
                if (results.length > 0) {
                    //保存Token
                    mytoken.setToken({ username: username, userid: results[0].userid }).then(token => {
                        res.send({ code: 200, msg: '登录成功', user: results[0], token: token });
                    });
                } else {
                    res.send({ code: 201, msg: '用户名或密码错误！' });
                }
            }
        });
    },
    regist: (req, res) => {
        let { username, pwd, truename, cardid, phone } = req.body;
        let head = '';
        if (req.file) {
            head = req.file.filename;
        }
        db.query(sql.users.regist, [username, pwd, truename, cardid, phone, head], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: '查询数据库错误！', err: err });
            } else {
                res.send({ code: 200, msg: '注册成功。' });
            }
        });
    },
    updatephone: (req, res) => {
        let { phone, oldphone } = req.body;
        let userid = req.body.userid;
        db.query(sql.users.updatephone, [phone, userid, oldphone], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: '查询数据库错误！', err: err });
            } else {
                if (results.affectedRows > 0) {
                    res.send({ code: 200, msg: '修改手机号成功。' });
                } else {
                    res.send({ code: 201, msg: '旧手机号不正确！' });
                }
            }
        });
    },
    updateemail: (req, res) => {
        let { email, oldemail } = req.body;
        let userid = req.body.userid;
        db.query(sql.users.updateemail, [email, userid, oldemail], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: '查询数据库错误！', err: err });
            } else {
                if (results.affectedRows > 0) {
                    res.send({ code: 200, msg: '修改邮箱成功。' });
                } else {
                    res.send({ code: 201, msg: '旧邮箱不正确！' });
                }
            }
        });
    },
    updatepwd: (req, res) => {
        let { pwd, oldpwd } = req.body;
        let userid = req.body.userid;
        db.query(sql.users.updatepwd, [pwd, userid, oldpwd], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: '查询数据库错误！', err: err });
            } else {
                if (results.affectedRows > 0) {
                    res.send({ code: 200, msg: '修改密码成功。' });
                } else {
                    res.send({ code: 201, msg: '旧密码不正确！' });
                }
            }
        });
    },
    use: (req, res) => {
        db.query(sql.users.use, (err, results) => {
            if (err) {
                res.send({ code: 500, msg: '查询数据库错误！', err: err });
            } else {
                res.send({ code: 200, msg: '查询成功', user: results });
            }
        })
    },
    updateuser: (req, res) => {
        // 确保接收所有必要字段
        let { userid, username, pwd, truename, cardid, phone, role } = req.body;

        // 参数顺序必须与SQL中的问号顺序完全一致
        db.query(
            sql.users.updateuser,
            [username, pwd, truename, cardid, phone, role, userid],
            (err, results) => {
                if (err) {
                    console.error("数据库错误:", err);
                    res.send({ code: 500, msg: '更新失败', err: err.message });
                } else {
                    res.send({
                        code: 200, msg: '修改成功', affectedRows: results.affectedRows
                    });
                }
            }
        );
    },
    adduser: (req, res) => {
        let { username, pwd, truename, cardid, phone, role,userid } = req.body;
        db.query(sql.users.adduser,[username, pwd, truename, cardid, phone, role,userid],
            (err, results) => {
                if (err) {
                    res.send({ code: 500, msg: '添加失败', err: err.message });
                } else {
                    res.send({ code: 200, msg: '添加成功', users: results });
                }
            })
    },
    deluser: (req, res) => {
        let  userid = req.body.userid;
        db.query(sql.users.deluser, [userid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, users: results });
            }   
        })
    }
};
